package cn.com.zd.demo.image;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

public class CreateNewImage {
    public static void main(String[] args) throws IOException {
        // 支持类型
        System.out.println(Arrays.asList(ImageIO.getReaderFormatNames()));
        System.out.println(Arrays.asList(ImageIO.getReaderFileSuffixes()));
        System.out.println(Arrays.asList(ImageIO.getReaderMIMETypes()));
        // 读取图片
//        Image image = Toolkit.getDefaultToolkit().getImage(new URL("https://pa.master336.cn:4433/pa/static/common/img/index/index3.jpg"));
        Image image = ImageIO.read(new URL("https://pa.master336.cn:4433/pa/static/common/img/index/index3.jpg"));
        // 生成图片
        BufferedImage bufferedImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_BGR);
        Graphics g = bufferedImage.getGraphics();
        try {
            g.drawImage(image,1,1,null);
//            g.fillRect(20, 40, 400, 200);
            g.setColor(new Color(120, 120, 120));
            g.setFont(new Font("隶书", Font.BOLD, 28));
            g.drawString("自定义图片", 200, 200);
            ImageIO.write(bufferedImage, "jpg", new File("D:/test.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            g.dispose();//释放画笔
        }
        try {
            // 读取原始图片
            BufferedImage originalImage = ImageIO.read(new File("d:/test0.jpg"));
            System.out.println("图片信息：\n高："+originalImage.getHeight()+"\t宽："+originalImage.getWidth());

            // 创建一个新的带有文本和图标的图片
            BufferedImage modifiedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = modifiedImage.createGraphics();
            graphics.drawImage(originalImage, 0, 0, null);

            // 添加文本
            String text = "Hello, World!";
            Font font = new Font("Arial", Font.BOLD, 20);

            graphics.setFont(font);
            graphics.setColor(Color.RED);
            graphics.drawString(text, 10, 30);

            Font font1 = new Font("华文彩云", Font.BOLD, 30);

            graphics.setFont(font1);
            graphics.setColor(Color.BLUE);
            graphics.drawString("哈哈~ 今天天气很好！", 10, 60);

            // 添加图标
            /*
            ImageIcon icon = new ImageIcon("d:/help.png");
            Image scaledIcon = icon.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT);
            graphics.drawImage(scaledIcon, 100, 50,scaledIcon.getWidth(null),scaledIcon.getHeight(null), null);

           Image imgtmp = Toolkit.getDefaultToolkit().getImage("d:/logo.jpg");
            graphics.drawImage(imgtmp,200,500,imgtmp.getWidth(null),imgtmp.getHeight(null),Color.RED,null);*/


            BufferedImage bufferimgpng = ImageIO.read(new File("d:/help.png"));
            graphics.drawImage(bufferimgpng, 100, 50, bufferimgpng.getWidth()/2,bufferimgpng.getHeight()/2,null);

            BufferedImage bufferimg = ImageIO.read(new File("d:/logo.jpg"));
            graphics.drawImage(bufferimg, 100, 50+bufferimgpng.getHeight()/2, bufferimg.getWidth()/3,bufferimg.getHeight()/3,null);

            // 保存修改后的图片
            ImageIO.write(modifiedImage, "jpg", new File("d:/test1.jpg"));

            // 显示修改后的图片
            JFrame frame = new JFrame();
            frame.getContentPane().setLayout(new FlowLayout());
//            frame.getContentPane().add(new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(new URL("https://pa.master336.cn:4433/pa/static/common/img/index/index3.jpg")))));
//            frame.getContentPane().add(new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage("d:/help.png"))));
//            frame.getContentPane().add(new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage("d:/logo.jpg"))));
            frame.getContentPane().add(new JLabel(new ImageIcon(modifiedImage)));
            frame.pack();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
